home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / Managed / DirectSound / AdjustSound / wfAdjust.cs < prev    next >
Encoding:
Text File  |  2004-09-27  |  40.7 KB  |  893 lines

  1. //----------------------------------------------------------------------------
  2. // File: wfAdjust.cs
  3. //
  4. // Copyright (c) Microsoft Corp. All rights reserved.
  5. //-----------------------------------------------------------------------------
  6. using System;
  7. using System.Drawing;
  8. using System.Collections;
  9. using System.ComponentModel;
  10. using System.Windows.Forms;
  11. using Microsoft.DirectX.DirectSound;
  12. using Buffer = Microsoft.DirectX.DirectSound.Buffer;
  13.  
  14. namespace csAdjustSound
  15. {
  16.     public class wfAdjust : System.Windows.Forms.Form
  17.     {
  18.         Device applicationDevice = null;
  19.         SecondaryBuffer applicationBuffer = null;
  20.         int lastGoodFrequency = 0;
  21.         const int maxFrequency = 200000; // The maximum frequency we'll allow this sample to support
  22.  
  23.         #region WindowsForms Variables
  24.         private System.Windows.Forms.Button buttonSound;
  25.         private System.Windows.Forms.TextBox textFile;
  26.         private System.Windows.Forms.TextBox textStatus;
  27.         private System.Windows.Forms.Label label1;
  28.         private System.Windows.Forms.Button buttonExit;
  29.         private System.Windows.Forms.Button buttonPlay;
  30.         private System.Windows.Forms.Button buttonStop;
  31.         private System.Windows.Forms.CheckBox checkLoop;
  32.         private System.Windows.Forms.Label label2;
  33.         private System.Windows.Forms.TextBox textFreq;
  34.         private System.Windows.Forms.TrackBar tbarFreq;
  35.         private System.Windows.Forms.Label label5;
  36.         private System.Windows.Forms.Label label6;
  37.         private System.Windows.Forms.TextBox textPan;
  38.         private System.Windows.Forms.Label label7;
  39.         private System.Windows.Forms.Label label8;
  40.         private System.Windows.Forms.Label label9;
  41.         private System.Windows.Forms.Label label10;
  42.         private System.Windows.Forms.GroupBox groupBox1;
  43.         private System.Timers.Timer tmrUpdate;
  44.         private System.Windows.Forms.Label label11;
  45.         private System.Windows.Forms.GroupBox groupBox2;
  46.         private System.Windows.Forms.RadioButton radioSticky;
  47.         private System.Windows.Forms.Label label13;
  48.         private System.Windows.Forms.RadioButton radioGlobal;
  49.         private System.Windows.Forms.RadioButton radioNormal;
  50.         private System.Windows.Forms.GroupBox groupBox4;
  51.         private System.Windows.Forms.RadioButton radioHardware;
  52.         private System.Windows.Forms.Label label12;
  53.         private System.Windows.Forms.RadioButton radioSoftware;
  54.         private System.Windows.Forms.RadioButton radioDefault;
  55.         private System.Windows.Forms.GroupBox groupBox5;
  56.         private System.Windows.Forms.Label lblBehavior;
  57.         private System.Windows.Forms.TrackBar tbarPan;
  58.         private System.Windows.Forms.TrackBar tbarVolume;
  59.         private System.Windows.Forms.OpenFileDialog ofdFile;
  60.         private System.Windows.Forms.TextBox textVolume;
  61.         private System.Windows.Forms.Label lblMaxFreq;
  62.         private System.Windows.Forms.Label lblMinFreq;
  63.         private System.ComponentModel.Container components = null;
  64.         #endregion
  65.  
  66.         public wfAdjust()
  67.         {
  68.             //
  69.             // Required for Windows Form Designer support
  70.             //
  71.             InitializeComponent();
  72.  
  73.             try
  74.             {
  75.                 // Load the icon from our resources
  76.                 System.Resources.ResourceManager resources = new System.Resources.ResourceManager(this.GetType());
  77.                 this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
  78.             }
  79.             catch
  80.             {
  81.                 // It's no big deal if we can't load our icons, but try to load the embedded one
  82.                 try { this.Icon = new System.Drawing.Icon(this.GetType(), "directx.ico"); } 
  83.                 catch {}
  84.             }
  85.  
  86.             try
  87.             {
  88.                 //Initialize DirectSound
  89.                 applicationDevice = new Device();
  90.                 applicationDevice.SetCooperativeLevel(this, CooperativeLevel.Priority);
  91.             }
  92.             catch
  93.             {
  94.                 MessageBox.Show("Could not initialize DirectSound.  Sample will exit.", "Exiting...", MessageBoxButtons.OK, MessageBoxIcon.Error);
  95.                 this.Close();
  96.                 throw;
  97.             }
  98.  
  99.             // Now that we have a sound device object set the frequency sliders correctly
  100.             this.tbarFreq.Minimum = 100;
  101.             this.tbarFreq.Maximum = maxFrequency;
  102.             this.lblMinFreq.Text = string.Format("{0} Hz", this.tbarFreq.Minimum);
  103.             this.lblMaxFreq.Text = string.Format("{0} KHz", this.tbarFreq.Maximum/1000);
  104.  
  105.             UpdateBehaviorText();
  106.         }
  107.  
  108.         /// <summary>
  109.         /// Clean up any resources being used.
  110.         /// </summary>
  111.         protected override void Dispose(bool disposing)
  112.         {
  113.             if (disposing)
  114.             {
  115.                 if (null != components) 
  116.                 {
  117.                     components.Dispose();
  118.                 }
  119.             }
  120.             base.Dispose(disposing);        
  121.         }
  122.  
  123.         #region Windows Form Designer generated code
  124.         /// <summary>
  125.         /// Required method for Designer support - do not modify
  126.         /// the contents of this method with the code editor.
  127.         /// </summary>
  128.         private void InitializeComponent()
  129.         {
  130.             this.radioDefault = new System.Windows.Forms.RadioButton();
  131.             this.tmrUpdate = new System.Timers.Timer();
  132.             this.checkLoop = new System.Windows.Forms.CheckBox();
  133.             this.textVolume = new System.Windows.Forms.TextBox();
  134.             this.label10 = new System.Windows.Forms.Label();
  135.             this.groupBox1 = new System.Windows.Forms.GroupBox();
  136.             this.groupBox4 = new System.Windows.Forms.GroupBox();
  137.             this.radioHardware = new System.Windows.Forms.RadioButton();
  138.             this.label12 = new System.Windows.Forms.Label();
  139.             this.radioSoftware = new System.Windows.Forms.RadioButton();
  140.             this.groupBox2 = new System.Windows.Forms.GroupBox();
  141.             this.radioSticky = new System.Windows.Forms.RadioButton();
  142.             this.label13 = new System.Windows.Forms.Label();
  143.             this.radioGlobal = new System.Windows.Forms.RadioButton();
  144.             this.radioNormal = new System.Windows.Forms.RadioButton();
  145.             this.textFile = new System.Windows.Forms.TextBox();
  146.             this.groupBox5 = new System.Windows.Forms.GroupBox();
  147.             this.lblBehavior = new System.Windows.Forms.Label();
  148.             this.buttonSound = new System.Windows.Forms.Button();
  149.             this.textStatus = new System.Windows.Forms.TextBox();
  150.             this.buttonPlay = new System.Windows.Forms.Button();
  151.             this.tbarPan = new System.Windows.Forms.TrackBar();
  152.             this.ofdFile = new System.Windows.Forms.OpenFileDialog();
  153.             this.buttonExit = new System.Windows.Forms.Button();
  154.             this.label11 = new System.Windows.Forms.Label();
  155.             this.tbarVolume = new System.Windows.Forms.TrackBar();
  156.             this.label8 = new System.Windows.Forms.Label();
  157.             this.label9 = new System.Windows.Forms.Label();
  158.             this.buttonStop = new System.Windows.Forms.Button();
  159.             this.lblMaxFreq = new System.Windows.Forms.Label();
  160.             this.label5 = new System.Windows.Forms.Label();
  161.             this.label6 = new System.Windows.Forms.Label();
  162.             this.label7 = new System.Windows.Forms.Label();
  163.             this.label1 = new System.Windows.Forms.Label();
  164.             this.label2 = new System.Windows.Forms.Label();
  165.             this.lblMinFreq = new System.Windows.Forms.Label();
  166.             this.textFreq = new System.Windows.Forms.TextBox();
  167.             this.tbarFreq = new System.Windows.Forms.TrackBar();
  168.             this.textPan = new System.Windows.Forms.TextBox();
  169.             ((System.ComponentModel.ISupportInitialize)(this.tmrUpdate)).BeginInit();
  170.             this.groupBox1.SuspendLayout();
  171.             this.groupBox4.SuspendLayout();
  172.             this.groupBox2.SuspendLayout();
  173.             this.groupBox5.SuspendLayout();
  174.             ((System.ComponentModel.ISupportInitialize)(this.tbarPan)).BeginInit();
  175.             ((System.ComponentModel.ISupportInitialize)(this.tbarVolume)).BeginInit();
  176.             ((System.ComponentModel.ISupportInitialize)(this.tbarFreq)).BeginInit();
  177.             this.SuspendLayout();
  178.             // 
  179.             // radioDefault
  180.             // 
  181.             this.radioDefault.Checked = true;
  182.             this.radioDefault.Location = new System.Drawing.Point(93, 16);
  183.             this.radioDefault.Name = "radioDefault";
  184.             this.radioDefault.Size = new System.Drawing.Size(79, 14);
  185.             this.radioDefault.TabIndex = 3;
  186.             this.radioDefault.TabStop = true;
  187.             this.radioDefault.Text = "Default";
  188.             this.radioDefault.CheckedChanged += new System.EventHandler(this.RadioChecked);
  189.             // 
  190.             // tmrUpdate
  191.             // 
  192.             this.tmrUpdate.Enabled = true;
  193.             this.tmrUpdate.SynchronizingObject = this;
  194.             this.tmrUpdate.Elapsed += new System.Timers.ElapsedEventHandler(this.tmrUpdate_Elapsed);
  195.             // 
  196.             // checkLoop
  197.             // 
  198.             this.checkLoop.Location = new System.Drawing.Point(8, 399);
  199.             this.checkLoop.Name = "checkLoop";
  200.             this.checkLoop.Size = new System.Drawing.Size(151, 19);
  201.             this.checkLoop.TabIndex = 3;
  202.             this.checkLoop.Text = "Loop Sound";
  203.             // 
  204.             // textVolume
  205.             // 
  206.             this.textVolume.Location = new System.Drawing.Point(85, 148);
  207.             this.textVolume.Name = "textVolume";
  208.             this.textVolume.ReadOnly = true;
  209.             this.textVolume.Size = new System.Drawing.Size(43, 20);
  210.             this.textVolume.TabIndex = 1;
  211.             this.textVolume.Text = "0";
  212.             // 
  213.             // label10
  214.             // 
  215.             this.label10.Location = new System.Drawing.Point(6, 140);
  216.             this.label10.Name = "label10";
  217.             this.label10.Size = new System.Drawing.Size(73, 38);
  218.             this.label10.TabIndex = 2;
  219.             this.label10.Text = "Volume";
  220.             this.label10.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
  221.             // 
  222.             // groupBox1
  223.             // 
  224.             this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
  225.                                                                                     this.groupBox4,
  226.                                                                                     this.groupBox2});
  227.             this.groupBox1.Location = new System.Drawing.Point(6, 181);
  228.             this.groupBox1.Name = "groupBox1";
  229.             this.groupBox1.Size = new System.Drawing.Size(437, 91);
  230.             this.groupBox1.TabIndex = 5;
  231.             this.groupBox1.TabStop = false;
  232.             this.groupBox1.Text = "Buffer Settings";
  233.             // 
  234.             // groupBox4
  235.             // 
  236.             this.groupBox4.Controls.AddRange(new System.Windows.Forms.Control[] {
  237.                                                                                     this.radioHardware,
  238.                                                                                     this.label12,
  239.                                                                                     this.radioSoftware,
  240.                                                                                     this.radioDefault});
  241.             this.groupBox4.Location = new System.Drawing.Point(8, 48);
  242.             this.groupBox4.Name = "groupBox4";
  243.             this.groupBox4.Size = new System.Drawing.Size(423, 36);
  244.             this.groupBox4.TabIndex = 6;
  245.             this.groupBox4.TabStop = false;
  246.             // 
  247.             // radioHardware
  248.             // 
  249.             this.radioHardware.Location = new System.Drawing.Point(173, 16);
  250.             this.radioHardware.Name = "radioHardware";
  251.             this.radioHardware.Size = new System.Drawing.Size(79, 14);
  252.             this.radioHardware.TabIndex = 3;
  253.             this.radioHardware.Text = "Hardware";
  254.             this.radioHardware.CheckedChanged += new System.EventHandler(this.RadioChecked);
  255.             // 
  256.             // label12
  257.             // 
  258.             this.label12.Location = new System.Drawing.Point(6, 13);
  259.             this.label12.Name = "label12";
  260.             this.label12.Size = new System.Drawing.Size(73, 15);
  261.             this.label12.TabIndex = 2;
  262.             this.label12.Text = "Buffer Mixing";
  263.             this.label12.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
  264.             // 
  265.             // radioSoftware
  266.             // 
  267.             this.radioSoftware.Location = new System.Drawing.Point(268, 17);
  268.             this.radioSoftware.Name = "radioSoftware";
  269.             this.radioSoftware.Size = new System.Drawing.Size(79, 14);
  270.             this.radioSoftware.TabIndex = 3;
  271.             this.radioSoftware.Text = "Software";
  272.             this.radioSoftware.CheckedChanged += new System.EventHandler(this.RadioChecked);
  273.             // 
  274.             // groupBox2
  275.             // 
  276.             this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
  277.                                                                                     this.radioSticky,
  278.                                                                                     this.label13,
  279.                                                                                     this.radioGlobal,
  280.                                                                                     this.radioNormal});
  281.             this.groupBox2.Location = new System.Drawing.Point(7, 11);
  282.             this.groupBox2.Name = "groupBox2";
  283.             this.groupBox2.Size = new System.Drawing.Size(423, 36);
  284.             this.groupBox2.TabIndex = 6;
  285.             this.groupBox2.TabStop = false;
  286.             // 
  287.             // radioSticky
  288.             // 
  289.             this.radioSticky.Location = new System.Drawing.Point(173, 16);
  290.             this.radioSticky.Name = "radioSticky";
  291.             this.radioSticky.Size = new System.Drawing.Size(79, 16);
  292.             this.radioSticky.TabIndex = 3;
  293.             this.radioSticky.Text = "Sticky";
  294.             this.radioSticky.CheckedChanged += new System.EventHandler(this.RadioChecked);
  295.             // 
  296.             // label13
  297.             // 
  298.             this.label13.Location = new System.Drawing.Point(6, 13);
  299.             this.label13.Name = "label13";
  300.             this.label13.Size = new System.Drawing.Size(73, 15);
  301.             this.label13.TabIndex = 2;
  302.             this.label13.Text = "Focus";
  303.             this.label13.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
  304.             // 
  305.             // radioGlobal
  306.             // 
  307.             this.radioGlobal.Location = new System.Drawing.Point(268, 17);
  308.             this.radioGlobal.Name = "radioGlobal";
  309.             this.radioGlobal.Size = new System.Drawing.Size(79, 14);
  310.             this.radioGlobal.TabIndex = 3;
  311.             this.radioGlobal.Text = "Global";
  312.             this.radioGlobal.CheckedChanged += new System.EventHandler(this.RadioChecked);
  313.             // 
  314.             // radioNormal
  315.             // 
  316.             this.radioNormal.Checked = true;
  317.             this.radioNormal.Location = new System.Drawing.Point(93, 16);
  318.             this.radioNormal.Name = "radioNormal";
  319.             this.radioNormal.Size = new System.Drawing.Size(79, 14);
  320.             this.radioNormal.TabIndex = 3;
  321.             this.radioNormal.TabStop = true;
  322.             this.radioNormal.Text = "Normal";
  323.             this.radioNormal.CheckedChanged += new System.EventHandler(this.RadioChecked);
  324.             // 
  325.             // textFile
  326.             // 
  327.             this.textFile.Location = new System.Drawing.Point(85, 6);
  328.             this.textFile.Name = "textFile";
  329.             this.textFile.ReadOnly = true;
  330.             this.textFile.Size = new System.Drawing.Size(350, 20);
  331.             this.textFile.TabIndex = 1;
  332.             this.textFile.Text = "";
  333.             // 
  334.             // groupBox5
  335.             // 
  336.             this.groupBox5.Controls.AddRange(new System.Windows.Forms.Control[] {
  337.                                                                                     this.lblBehavior});
  338.             this.groupBox5.Location = new System.Drawing.Point(8, 278);
  339.             this.groupBox5.Name = "groupBox5";
  340.             this.groupBox5.Size = new System.Drawing.Size(431, 120);
  341.             this.groupBox5.TabIndex = 6;
  342.             this.groupBox5.TabStop = false;
  343.             this.groupBox5.Text = "Expected Behavior";
  344.             // 
  345.             // lblBehavior
  346.             // 
  347.             this.lblBehavior.Location = new System.Drawing.Point(6, 16);
  348.             this.lblBehavior.Name = "lblBehavior";
  349.             this.lblBehavior.Size = new System.Drawing.Size(422, 100);
  350.             this.lblBehavior.TabIndex = 0;
  351.             this.lblBehavior.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
  352.             // 
  353.             // buttonSound
  354.             // 
  355.             this.buttonSound.Location = new System.Drawing.Point(3, 6);
  356.             this.buttonSound.Name = "buttonSound";
  357.             this.buttonSound.Size = new System.Drawing.Size(74, 21);
  358.             this.buttonSound.TabIndex = 0;
  359.             this.buttonSound.Text = "Sound File...";
  360.             this.buttonSound.Click += new System.EventHandler(this.buttonSound_Click);
  361.             // 
  362.             // textStatus
  363.             // 
  364.             this.textStatus.Location = new System.Drawing.Point(85, 33);
  365.             this.textStatus.Name = "textStatus";
  366.             this.textStatus.ReadOnly = true;
  367.             this.textStatus.Size = new System.Drawing.Size(350, 20);
  368.             this.textStatus.TabIndex = 1;
  369.             this.textStatus.Text = "No File Loaded.";
  370.             // 
  371.             // buttonPlay
  372.             // 
  373.             this.buttonPlay.Enabled = false;
  374.             this.buttonPlay.Location = new System.Drawing.Point(7, 421);
  375.             this.buttonPlay.Name = "buttonPlay";
  376.             this.buttonPlay.Size = new System.Drawing.Size(74, 21);
  377.             this.buttonPlay.TabIndex = 0;
  378.             this.buttonPlay.Text = "Play";
  379.             this.buttonPlay.Click += new System.EventHandler(this.buttonPlay_Click);
  380.             // 
  381.             // tbarPan
  382.             // 
  383.             this.tbarPan.Location = new System.Drawing.Point(164, 97);
  384.             this.tbarPan.Maximum = 20;
  385.             this.tbarPan.Minimum = -20;
  386.             this.tbarPan.Name = "tbarPan";
  387.             this.tbarPan.Size = new System.Drawing.Size(236, 42);
  388.             this.tbarPan.TabIndex = 4;
  389.             this.tbarPan.TickFrequency = 5;
  390.             this.tbarPan.Scroll += new System.EventHandler(this.tbarPan_Scroll);
  391.             // 
  392.             // ofdFile
  393.             // 
  394.             this.ofdFile.Filter = "Wave Files (*.wav)|*.wav|All Files (*.*)|*.*";
  395.             this.ofdFile.Title = "Open Audio File";
  396.             // 
  397.             // buttonExit
  398.             // 
  399.             this.buttonExit.DialogResult = System.Windows.Forms.DialogResult.Cancel;
  400.             this.buttonExit.Location = new System.Drawing.Point(362, 421);
  401.             this.buttonExit.Name = "buttonExit";
  402.             this.buttonExit.Size = new System.Drawing.Size(74, 21);
  403.             this.buttonExit.TabIndex = 0;
  404.             this.buttonExit.Text = "Exit";
  405.             this.buttonExit.Click += new System.EventHandler(this.buttonExit_Click);
  406.             // 
  407.             // label11
  408.             // 
  409.             this.label11.Location = new System.Drawing.Point(6, 13);
  410.             this.label11.Name = "label11";
  411.             this.label11.Size = new System.Drawing.Size(73, 15);
  412.             this.label11.TabIndex = 2;
  413.             this.label11.Text = "Focus";
  414.             this.label11.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
  415.             // 
  416.             // tbarVolume
  417.             // 
  418.             this.tbarVolume.Location = new System.Drawing.Point(165, 140);
  419.             this.tbarVolume.Maximum = 0;
  420.             this.tbarVolume.Minimum = -50;
  421.             this.tbarVolume.Name = "tbarVolume";
  422.             this.tbarVolume.Size = new System.Drawing.Size(236, 42);
  423.             this.tbarVolume.TabIndex = 4;
  424.             this.tbarVolume.Scroll += new System.EventHandler(this.tbarVolume_Scroll);
  425.             // 
  426.             // label8
  427.             // 
  428.             this.label8.Location = new System.Drawing.Point(131, 147);
  429.             this.label8.Name = "label8";
  430.             this.label8.Size = new System.Drawing.Size(41, 20);
  431.             this.label8.TabIndex = 2;
  432.             this.label8.Text = "Low";
  433.             this.label8.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
  434.             // 
  435.             // label9
  436.             // 
  437.             this.label9.Location = new System.Drawing.Point(396, 149);
  438.             this.label9.Name = "label9";
  439.             this.label9.Size = new System.Drawing.Size(47, 20);
  440.             this.label9.TabIndex = 2;
  441.             this.label9.Text = "High";
  442.             this.label9.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
  443.             // 
  444.             // buttonStop
  445.             // 
  446.             this.buttonStop.Enabled = false;
  447.             this.buttonStop.Location = new System.Drawing.Point(87, 421);
  448.             this.buttonStop.Name = "buttonStop";
  449.             this.buttonStop.Size = new System.Drawing.Size(74, 21);
  450.             this.buttonStop.TabIndex = 0;
  451.             this.buttonStop.Text = "Stop";
  452.             this.buttonStop.Click += new System.EventHandler(this.buttonStop_Click);
  453.             // 
  454.             // lblMaxFreq
  455.             // 
  456.             this.lblMaxFreq.Location = new System.Drawing.Point(396, 68);
  457.             this.lblMaxFreq.Name = "lblMaxFreq";
  458.             this.lblMaxFreq.Size = new System.Drawing.Size(47, 20);
  459.             this.lblMaxFreq.TabIndex = 2;
  460.             this.lblMaxFreq.Text = "100 KHz";
  461.             this.lblMaxFreq.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
  462.             // 
  463.             // label5
  464.             // 
  465.             this.label5.Location = new System.Drawing.Point(130, 104);
  466.             this.label5.Name = "label5";
  467.             this.label5.Size = new System.Drawing.Size(41, 20);
  468.             this.label5.TabIndex = 2;
  469.             this.label5.Text = "Left";
  470.             this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
  471.             // 
  472.             // label6
  473.             // 
  474.             this.label6.Location = new System.Drawing.Point(395, 106);
  475.             this.label6.Name = "label6";
  476.             this.label6.Size = new System.Drawing.Size(47, 20);
  477.             this.label6.TabIndex = 2;
  478.             this.label6.Text = "Right";
  479.             this.label6.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
  480.             // 
  481.             // label7
  482.             // 
  483.             this.label7.Location = new System.Drawing.Point(5, 97);
  484.             this.label7.Name = "label7";
  485.             this.label7.Size = new System.Drawing.Size(73, 38);
  486.             this.label7.TabIndex = 2;
  487.             this.label7.Text = "Pan";
  488.             this.label7.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
  489.             // 
  490.             // label1
  491.             // 
  492.             this.label1.Location = new System.Drawing.Point(4, 33);
  493.             this.label1.Name = "label1";
  494.             this.label1.Size = new System.Drawing.Size(73, 20);
  495.             this.label1.TabIndex = 2;
  496.             this.label1.Text = "Status";
  497.             this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
  498.             // 
  499.             // label2
  500.             // 
  501.             this.label2.Location = new System.Drawing.Point(6, 59);
  502.             this.label2.Name = "label2";
  503.             this.label2.Size = new System.Drawing.Size(73, 38);
  504.             this.label2.TabIndex = 2;
  505.             this.label2.Text = "Frequency";
  506.             this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
  507.             // 
  508.             // lblMinFreq
  509.             // 
  510.             this.lblMinFreq.Location = new System.Drawing.Point(131, 66);
  511.             this.lblMinFreq.Name = "lblMinFreq";
  512.             this.lblMinFreq.Size = new System.Drawing.Size(41, 20);
  513.             this.lblMinFreq.TabIndex = 2;
  514.             this.lblMinFreq.Text = "100 Hz";
  515.             this.lblMinFreq.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
  516.             // 
  517.             // textFreq
  518.             // 
  519.             this.textFreq.Location = new System.Drawing.Point(85, 67);
  520.             this.textFreq.Name = "textFreq";
  521.             this.textFreq.ReadOnly = true;
  522.             this.textFreq.Size = new System.Drawing.Size(43, 20);
  523.             this.textFreq.TabIndex = 1;
  524.             this.textFreq.Text = "0";
  525.             // 
  526.             // tbarFreq
  527.             // 
  528.             this.tbarFreq.LargeChange = 1000;
  529.             this.tbarFreq.Location = new System.Drawing.Point(165, 59);
  530.             this.tbarFreq.Maximum = 100000;
  531.             this.tbarFreq.Minimum = 100;
  532.             this.tbarFreq.Name = "tbarFreq";
  533.             this.tbarFreq.Size = new System.Drawing.Size(236, 42);
  534.             this.tbarFreq.SmallChange = 100;
  535.             this.tbarFreq.TabIndex = 4;
  536.             this.tbarFreq.TickFrequency = 10000;
  537.             this.tbarFreq.Value = 100;
  538.             this.tbarFreq.Scroll += new System.EventHandler(this.tbarFreq_Scroll);
  539.             // 
  540.             // textPan
  541.             // 
  542.             this.textPan.Location = new System.Drawing.Point(85, 105);
  543.             this.textPan.Name = "textPan";
  544.             this.textPan.ReadOnly = true;
  545.             this.textPan.Size = new System.Drawing.Size(43, 20);
  546.             this.textPan.TabIndex = 1;
  547.             this.textPan.Text = "0";
  548.             // 
  549.             // wfAdjust
  550.             // 
  551.             this.AcceptButton = this.buttonSound;
  552.             this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  553.             this.CancelButton = this.buttonExit;
  554.             this.ClientSize = new System.Drawing.Size(460, 448);
  555.             this.Controls.AddRange(new System.Windows.Forms.Control[] {
  556.                                                                           this.groupBox5,
  557.                                                                           this.groupBox1,
  558.                                                                           this.textVolume,
  559.                                                                           this.label8,
  560.                                                                           this.label9,
  561.                                                                           this.label10,
  562.                                                                           this.tbarVolume,
  563.                                                                           this.label5,
  564.                                                                           this.label6,
  565.                                                                           this.tbarPan,
  566.                                                                           this.textPan,
  567.                                                                           this.label7,
  568.                                                                           this.lblMaxFreq,
  569.                                                                           this.lblMinFreq,
  570.                                                                           this.textFreq,
  571.                                                                           this.tbarFreq,
  572.                                                                           this.label2,
  573.                                                                           this.checkLoop,
  574.                                                                           this.buttonStop,
  575.                                                                           this.buttonPlay,
  576.                                                                           this.buttonExit,
  577.                                                                           this.label1,
  578.                                                                           this.textStatus,
  579.                                                                           this.textFile,
  580.                                                                           this.buttonSound});
  581.             this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
  582.             this.MaximizeBox = false;
  583.             this.Name = "wfAdjust";
  584.             this.Text = "AdjustSound";
  585.             ((System.ComponentModel.ISupportInitialize)(this.tmrUpdate)).EndInit();
  586.             this.groupBox1.ResumeLayout(false);
  587.             this.groupBox4.ResumeLayout(false);
  588.             this.groupBox2.ResumeLayout(false);
  589.             this.groupBox5.ResumeLayout(false);
  590.             ((System.ComponentModel.ISupportInitialize)(this.tbarPan)).EndInit();
  591.             ((System.ComponentModel.ISupportInitialize)(this.tbarVolume)).EndInit();
  592.             ((System.ComponentModel.ISupportInitialize)(this.tbarFreq)).EndInit();
  593.             this.ResumeLayout(false);
  594.  
  595.         }
  596.         #endregion
  597.  
  598.         /// <summary>
  599.         /// The main entry point for the application.
  600.         /// </summary>
  601.         static void Main() 
  602.         {
  603.             try
  604.             {
  605.                 using (wfAdjust form = new wfAdjust())
  606.                 {
  607.                     Application.Run(form);    
  608.                 }
  609.             }
  610.             catch{} 
  611.         }
  612.  
  613.         private void buttonExit_Click(object sender, System.EventArgs e)
  614.         {
  615.             this.Dispose();
  616.         }
  617.  
  618.         private void buttonSound_Click(object sender, System.EventArgs e)
  619.         {
  620.             bool FocusSticky = radioSticky.Checked;
  621.             bool FocusGlobal = radioGlobal.Checked;
  622.             bool MixHardware = radioHardware.Checked;
  623.             bool MixSoftware = radioSoftware.Checked;
  624.  
  625.             // Make sure we're stopped
  626.             this.buttonStop_Click(null, null);
  627.             textStatus.Text = "Loading file...";
  628.             if (null != applicationBuffer)
  629.                 applicationBuffer.Dispose();
  630.             applicationBuffer = null;
  631.  
  632.             tbarFreq.Value = tbarFreq.Minimum;
  633.             tbarPan.Value = 0;
  634.             tbarVolume.Value = 0;
  635.             // Show the open file dialog and let's load the file
  636.             if (null == ofdFile.InitialDirectory)
  637.             {
  638.                 // Default to the 'My documents' folder if it's available
  639.                 ofdFile.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
  640.             }
  641.             // Clear out any old file name that may be in there
  642.             ofdFile.FileName = null;
  643.             if (DialogResult.OK == ofdFile.ShowDialog())
  644.             {
  645.                 // Save the initial dir as the last one picked.
  646.                 ofdFile.InitialDirectory = System.IO.Path.GetDirectoryName(ofdFile.FileName);
  647.                 try
  648.                 {
  649.                     BufferDescription desc = new BufferDescription();
  650.                     
  651.                     desc.ControlFrequency  = true;
  652.                     desc.ControlPan = true;
  653.                     desc.ControlVolume = true;
  654.  
  655.                     if (FocusGlobal)
  656.                         desc.GlobalFocus = true;
  657.  
  658.                     if (FocusSticky)
  659.                         desc.StickyFocus = true;
  660.  
  661.                     if (MixHardware)
  662.                         desc.LocateInHardware = true;
  663.  
  664.                     if (MixSoftware)
  665.                         desc.LocateInSoftware = true;
  666.  
  667.                     applicationBuffer = new SecondaryBuffer(ofdFile.FileName, desc, applicationDevice);
  668.                     tbarFreq.Value = desc.Format.SamplesPerSecond;
  669.                     lastGoodFrequency = desc.Format.SamplesPerSecond;
  670.                     textFile.Text = ofdFile.FileName;
  671.                     textFreq.Text = tbarFreq.Value.ToString();
  672.                     textStatus.Text = "File loaded.";
  673.                     EnablePlayUI(true);
  674.                 }
  675.                 catch (Exception de)
  676.                 {
  677.                     Console.WriteLine(de.ToString());
  678.                     textFile.Text = null;
  679.                     textStatus.Text = "Could not load this segment.";
  680.                     DefaultPlayUI();
  681.                 }
  682.             }
  683.             else
  684.             {
  685.                 textFile.Text = null;
  686.                 textStatus.Text = "Load aborted.";
  687.                 DefaultPlayUI();
  688.             }
  689.         }
  690.  
  691.         private void buttonStop_Click(object sender, System.EventArgs e)
  692.         {
  693.             if (null != applicationBuffer)
  694.             {
  695.                 textStatus.Text = "Sound stopped.";
  696.                 EnablePlayUI(true);
  697.                 applicationBuffer.Stop();
  698.                 applicationBuffer.SetCurrentPosition(0);
  699.             }
  700.         }
  701.  
  702.         private void tbarFreq_Scroll(object sender, System.EventArgs e)
  703.         {
  704.             int newFrequency = 0;
  705.             if (null != applicationBuffer)
  706.             {
  707.                 try
  708.                 {
  709.                     newFrequency = ((TrackBar)sender).Value;
  710.                     // Attempt to set the frequency to the new value
  711.                     applicationBuffer.Frequency = newFrequency;
  712.                     textFreq.Text = newFrequency.ToString();
  713.                     lastGoodFrequency = newFrequency;
  714.                 }
  715.                 catch 
  716.                 {
  717.                     // Let's try to guess why it failed..
  718.                     if ((applicationBuffer.Caps.LocateInHardware) && (newFrequency > applicationDevice.Caps.MaxSecondarySampleRate))
  719.                     {
  720.                         textStatus.Text = "Hardware buffers don't support greater than Caps.MaxSecondarySampleRate";
  721.                     }
  722.                     else if (100000 < newFrequency)
  723.                     {
  724.                         // Some platforms (pre-WinXP SP1) don't support 
  725.                         // >100k Hz so they will fail when setting it higher
  726.                         textStatus.Text = "Some OS platforms do not support >100k Hz";
  727.                     }
  728.                     else
  729.                     {
  730.                         textStatus.Text = "Setting the frequency failed";
  731.                     }
  732.                     // Reset to the last valid frequency
  733.                     applicationBuffer.Frequency = lastGoodFrequency;
  734.                     ((TrackBar)sender).Value = lastGoodFrequency;
  735.                 }
  736.             }
  737.         }
  738.  
  739.         private void buttonPlay_Click(object sender, System.EventArgs e)
  740.         {
  741.             bool FocusSticky = radioSticky.Checked;
  742.             bool FocusGlobal = radioGlobal.Checked;
  743.             bool MixHardware = radioHardware.Checked;
  744.             bool MixSoftware = radioSoftware.Checked;
  745.  
  746.             if (null != applicationBuffer)
  747.             {
  748.                 textStatus.Text = "Sound playing.";
  749.                 EnablePlayUI(false);
  750.                 Application.DoEvents(); // Process the Stop click that EnablePlayUI generates.
  751.  
  752.                 // First we need to 'recreate' the buffer
  753.                 if (null != applicationBuffer)
  754.                     applicationBuffer.Dispose();
  755.                 applicationBuffer = null;
  756.  
  757.                 BufferDescription desc = new BufferDescription();
  758.                 desc.ControlFrequency  = true;
  759.                 desc.ControlPan = true;
  760.                 desc.ControlVolume = true;
  761.  
  762.                 if (FocusGlobal)
  763.                     desc.GlobalFocus = true;
  764.  
  765.                 if (FocusSticky)
  766.                     desc.StickyFocus = true;
  767.  
  768.                 if (MixHardware)
  769.                     desc.LocateInHardware = true;
  770.  
  771.                 if (MixSoftware)
  772.                     desc.LocateInSoftware = true;
  773.  
  774.                 try
  775.                 {
  776.                     applicationBuffer = new SecondaryBuffer(ofdFile.FileName, desc, applicationDevice);
  777.  
  778.                     BufferPlayFlags PlayFlags  = checkLoop.Checked ? BufferPlayFlags.Looping : 0;
  779.  
  780.                     // Before we play, make sure we're using the correct settings
  781.                     tbarFreq_Scroll(tbarFreq, null);
  782.                     tbarPan_Scroll(tbarPan, null);
  783.                     tbarVolume_Scroll(tbarVolume, null);
  784.                     applicationBuffer.Play(0, PlayFlags);
  785.                 }
  786.                 catch
  787.                 {
  788.                     textStatus.Text = "Could not open this file with these settings.";
  789.                     DefaultPlayUI();
  790.                 }
  791.             }
  792.         }
  793.  
  794.         private void tbarPan_Scroll(object sender, System.EventArgs e)
  795.         {
  796.             if (null != applicationBuffer)
  797.             {
  798.                 textPan.Text = ((TrackBar)sender).Value.ToString();
  799.                 applicationBuffer.Pan = ((TrackBar)sender).Value * 500;
  800.             }
  801.         }
  802.  
  803.         private void tbarVolume_Scroll(object sender, System.EventArgs e)
  804.         {
  805.             if (null != applicationBuffer)
  806.             {
  807.                 textVolume.Text = ((TrackBar)sender).Value.ToString();
  808.                 applicationBuffer.Volume = ((TrackBar)sender).Value * 100;
  809.             }
  810.         }
  811.         private void EnablePlayUI(bool bEnable)
  812.         {
  813.             buttonPlay.Enabled = bEnable;
  814.             buttonStop.Enabled = !bEnable;
  815.             buttonSound.Enabled = bEnable;
  816.             checkLoop.Enabled = bEnable;
  817.             radioDefault.Enabled = bEnable;
  818.             radioGlobal.Enabled = bEnable;
  819.             radioHardware.Enabled = bEnable;
  820.             radioNormal.Enabled = bEnable;
  821.             radioSoftware.Enabled = bEnable;
  822.             radioSticky.Enabled = bEnable;
  823.         }
  824.         private void DefaultPlayUI()
  825.         {
  826.             buttonPlay.Enabled = false;
  827.             buttonStop.Enabled = false;
  828.             buttonSound.Enabled = true;
  829.             checkLoop.Enabled = false;
  830.             radioDefault.Enabled = true;
  831.             radioGlobal.Enabled = true;
  832.             radioHardware.Enabled = true;
  833.             radioNormal.Enabled = true;
  834.             radioSoftware.Enabled = true;
  835.             radioSticky.Enabled = true;
  836.             textFile.Text = null;
  837.         }
  838.         private void tmrUpdate_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
  839.         {
  840.             if (null != applicationBuffer)
  841.             {
  842.                 if (false == applicationBuffer.Status.Playing && false == applicationBuffer.Status.Looping)
  843.                     buttonStop_Click(null, null);
  844.             }
  845.         }
  846.         private void UpdateBehaviorText()
  847.         {
  848.             string sText = null;
  849.             bool Looped      = checkLoop.Checked;
  850.             bool FocusSticky = radioSticky.Checked;
  851.             bool FocusGlobal = radioGlobal.Checked;
  852.             bool MixHardware = radioHardware.Checked;
  853.             bool MixSoftware = radioSoftware.Checked;
  854.  
  855.             // Figure what the user should expect based on the dialog choice
  856.             if (FocusSticky)
  857.             {
  858.                 sText = "Buffers with \"sticky\" focus will continue to play if the user switches to another application not using DirectSound.  However, if the user switches to another DirectSound application, all normal-focus and sticky-focus buffers in the previous application are muted.";
  859.  
  860.             }
  861.             else if (FocusGlobal)
  862.             {
  863.                 sText = "Buffers with global focus will continue to play if the user switches focus to another application, even if the new application uses DirectSound. The one exception is if you switch focus to a DirectSound application that uses the DSSCL_WRITEPRIMARY cooperative level. In this case, the global-focus buffers from other applications will not be audible.";
  864.             }
  865.             else
  866.             {
  867.                 // Normal focus
  868.                 sText = "Buffers with normal focus will mute if the user switches focus to any other application";
  869.             }
  870.  
  871.             if (MixHardware)
  872.             {
  873.                 sText = sText + "\n\nWith the hardware mixing flag, the new buffer will be forced to use hardware mixing. If the device does not support hardware mixing or if the required hardware resources are not available, the call to the DirectSound.CreateSoundBuffer method will fail."; 
  874.             }
  875.             else if (MixSoftware)
  876.             {
  877.                 sText = sText + "\n\nWith the software mixing flag, the new buffer will use software mixing, even if hardware resources are available.";
  878.             }
  879.             else 
  880.             {
  881.                 // Default mixing
  882.                 sText = sText + "\n\nWith default mixing, the new buffer will use hardware mixing if available, otherwise software mixing will be used."; 
  883.             }
  884.             lblBehavior.Text = sText;
  885.         }
  886.  
  887.         private void RadioChecked(object sender, System.EventArgs e)
  888.         {
  889.             UpdateBehaviorText();
  890.         }
  891.     }
  892. }
  893.